package com.android.zweibo.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.android.zweibo.bean.Common;
import com.android.zweibo.bean.UserInfo;

public class SharedPreferenceUtil {
	
	
    /**
     * 保存用户登录信息
     * @param context
     * @param nowUser
     */
	public static void saveLoginUser(Context context,
			UserInfo nowUser) {
		SharedPreferences sp = context.getSharedPreferences(Common.NOWUSER_SHARED, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putString(UserInfo.USER_ID, nowUser.getUserId());
		editor.putString(UserInfo.USER_NAME, nowUser.getUserName());
		editor.putString(UserInfo.TOKEN, nowUser.getToken());
		editor.putString(UserInfo.TOKEN_SECRET, nowUser.getTokenSecret());
		editor.commit();
	}
	
    /**
     * 获取保存的当前登录的用户信息
     * @param context
     * @return
     */
	public static UserInfo getLoginUser(Context context) {
		SharedPreferences sp  = context.getSharedPreferences(Common.NOWUSER_SHARED,Context.MODE_PRIVATE );
		String userId = sp.getString(UserInfo.USER_ID, null);
		String userName = sp.getString(UserInfo.USER_NAME, null);
		String token = sp.getString(UserInfo.TOKEN, null);
		String token_secret = sp.getString(UserInfo.TOKEN_SECRET, null);
		
		if(null == userId) {
			return null;
		}
		
		return new UserInfo(userId, userName, token, token_secret, "1");
	}

}
